from base import build_request, Yaml, Path, Json
from page.nsp_user_api import NspUserApi


class NspShelvesApi(NspUserApi):
    """
    用途：在接口端管理员用户操作商品上下架方法
    作者：曹峰
    时间：2019-5-6
    邮箱：243934857@qq.com
    """

    __shelves_yaml = Yaml().get_config_as_dict(Path.get_actual_path_by_current_file(__file__, 'nsp.yaml'))[
        'NspShelvesApi']

    def add(self, id, sign=None, token=None):
        """
        商品上架
        :param id: 商品id
        :param sign: 用户签名
        :param token: token
        :return: 返回基本值ret、data、msg和需要的updated_count、state、update_time
        """
        sign, token = self._get_to_token_sign(sign, token)

        url = self._base_url
        add_url = '%s?s=%s' % (url, self.__shelves_yaml['ADD_URL'])
        add_data = {'sign': sign, 'id': id}
        add_req = build_request(add_url, 'p', auth=token, params=add_data)

        return self._get_add_detail_resp(add_req.json_dict)

    def _get_add_detail_resp(self, json_dict):
        """
        解析body返回值
        :param json_dict:
        :return: 返回基本值ret、data、msg；
                   需要的 updated_count、state、update_time
        """
        detail_resp = self._get_parse_http_resp(json_dict)

        detail_resp['updated_count'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__shelves_yaml[
                'ADD_COUNT_DATAKEY'],
            index=self.__shelves_yaml[
                'ADD_COUNT_INDEX'],
            sub_key=self.__shelves_yaml[
                'ADD_COUNT_SUBKEY']
        )

        detail_resp['state'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__shelves_yaml[
                'ADD_STATE_DATAKEY'],
            index=self.__shelves_yaml[
                'ADD_STATE_INDEX'],
            sub_key=self.__shelves_yaml[
                'ADD_STATE_SUBKEY']
        )

        detail_resp['update_time'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__shelves_yaml[
                'ADD_UPDATE_KEY'],
            index=self.__shelves_yaml[
                'ADD_UPDATE_INDEX'],
            sub_key=self.__shelves_yaml[
                'ADD_UPDATE_SUBKEY']
        )

        detail_resp['json_dict'] = json_dict

        return detail_resp

    def move(self, id, sign=None, token=None):
        """
        商品下架
        :param id: 商品id
        :param sign: 用户签名
        :param token: token
        :return: 返回基本值ret、data、msg
                     和需要的updated_count、state、update_time
        """
        sign, token = self._get_to_token_sign(sign, token)

        url = self._base_url
        move_url = '%s?s=%s' % (url, self.__shelves_yaml['MOVE_URL'])
        move_data = {'sign': sign, 'id': id}
        move_req = build_request(move_url, 'p', auth=token, params=move_data)

        return self._get_move_detail_resp(move_req.json_dict)

    def _get_move_detail_resp(self, json_dict):
        """
        解析body返回值
        :param json_dict:
        :return: 返回基本值ret、data、msg；
                   需要的 updated_count、state、update_time
        """
        detail_resp = self._get_parse_http_resp(json_dict)
        detail_resp['updated_count'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__shelves_yaml[
                'COUNT_DATAKEY'],
            index=self.__shelves_yaml[
                'COUNT_INDEX'],
            sub_key=self.__shelves_yaml[
                'COUNT_SUBKEY']
        )

        detail_resp['state'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__shelves_yaml[
                'STATE_DATAKEY'],
            index=self.__shelves_yaml[
                'STATE_INDEX'],
            sub_key=self.__shelves_yaml[
                'STATE_SUBKEY']
        )

        detail_resp['update_time'] = Json.parse_json_dict_value(
            json_dict,
            data_key=self.__shelves_yaml[
                'UPDATE_KEY'],
            index=self.__shelves_yaml[
                'UPDATE_INDEX'],
            sub_key=self.__shelves_yaml[
                'UPDATE_SUBKEY']
        )

        detail_resp['json_dict'] = json_dict

        return detail_resp
